package com.eeds.service.word;

import com.eeds.common.enums.TemplateEnum;
import com.eeds.common.model.RespEnum;
import com.eeds.common.table.ProAdminEntity;
import com.eeds.common.table.SysFilesEntity;
import com.eeds.dao.ProReExaminationDao;
import com.eeds.dao.SysFilesDao;
import com.eeds.exception.ServerException;
import com.eeds.json.GsonUtil;
import com.eeds.model.word.req.ReqExportDocVO;
import com.eeds.model.word.res.ResExaminationVO;
import com.eeds.model.word.res.ResExportDocVO;
import com.eeds.model.word.res.ResPicVO;
import com.eeds.word.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
@Service
public class WordServiceImpl implements WordService{

    @Value("${file.base.path}")
    private String fileBasePath;

    @Autowired
    private ProReExaminationDao proReExaminationDao;

    @Autowired
    private SysFilesDao sysFilesDao;


    /**
     * 导出doc
     *
     * @param reqExportDocVO
     * @return
     */
    @Override
    public String exportDoc(ReqExportDocVO reqExportDocVO){
        ResExportDocVO resExportDocVO = new ResExportDocVO();
        switch (reqExportDocVO.getTemplateEnum()){
            case TEMPLATE_SUPPLY_SCHEME:
                ProAdminEntity proAdminEntity = new ProAdminEntity();
                proAdminEntity.setCustomerName("徐海硕");
                resExportDocVO.setProject(proAdminEntity);
                break;
            case TEMPLATE_EXAMINATION:
                resExportDocVO.setProject(getExamination(reqExportDocVO.getIdProAdmin()));
                break;
        }
        return exportDoc(resExportDocVO,reqExportDocVO.getTemplateEnum());
    }

    /**
     * 导出doc
     *
     * @param object
     * @param templateEnum
     * @return
     */
    @Override
    public String exportDoc(Object object, TemplateEnum templateEnum){
        return WordUtil.createWord(GsonUtil.GsonToMaps(GsonUtil.GsonString(object)),templateEnum.getDescription(),fileBasePath);
    }

    /**
     * 导出doc 文件流
     * @param filePath
     * @param outputStream
     * @return
     */
    @Override
    public void exportDoc(String filePath,OutputStream outputStream){
        InputStream input = null;
        try {
            input = new FileInputStream(new File(filePath));
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) != -1) {
                outputStream.write(buf, 0, bytesRead);
            }
        }catch (Exception e){
            throw new ServerException(RespEnum.SERVER_ERROR,e);
        }finally {
            try {
                if(input != null){
                    input.close();
                }
            }catch (IOException e){
                throw new ServerException(RespEnum.SERVER_ERROR,e);
            }
        }
    }

    private ResExaminationVO getExamination(Long idProAdmin){
        ResExaminationVO resExaminationVO  = proReExaminationDao.getExaminationById(idProAdmin);
        if(resExaminationVO != null){

            ResPicVO resPicVO = new ResPicVO();
            SysFilesEntity sysFilesEntity = sysFilesDao.selectById(resExaminationVO.getOrganizationalUnitChargeId());
            resPicVO.setId(resExaminationVO.getOrganizationalUnitChargeId());
            resPicVO.setPicPath(sysFilesEntity.getFilePath());
            resPicVO.setData(WordUtil.getImageString(sysFilesEntity.getFilePath()));
            resExaminationVO.setOrganizationalUnitCharge(resPicVO);


            resPicVO = new ResPicVO();
            sysFilesEntity = sysFilesDao.selectById(resExaminationVO.getConstructionPersonChargeId());
            resPicVO.setId(resExaminationVO.getConstructionPersonChargeId());
            resPicVO.setPicPath(sysFilesEntity.getFilePath());
            resPicVO.setData(WordUtil.getImageString(sysFilesEntity.getFilePath()));
            resExaminationVO.setConstructionPersonCharge(resPicVO);


            resPicVO = new ResPicVO();
            sysFilesEntity = sysFilesDao.selectById(resExaminationVO.getAcceptanceUnitChargeId());
            resPicVO.setId(resExaminationVO.getAcceptanceUnitChargeId());
            resPicVO.setPicPath(sysFilesEntity.getFilePath());
            resPicVO.setData(WordUtil.getImageString(sysFilesEntity.getFilePath()));
            resExaminationVO.setAcceptanceUnitCharge(resPicVO);


            resPicVO = new ResPicVO();
            sysFilesEntity = sysFilesDao.selectById(resExaminationVO.getCustomerPersonChargeId());
            resPicVO.setId(resExaminationVO.getCustomerPersonChargeId());
            resPicVO.setPicPath(sysFilesEntity.getFilePath());
            resPicVO.setData(WordUtil.getImageString(sysFilesEntity.getFilePath()));
            resExaminationVO.setCustomerPersonCharge(resPicVO);
        }
        return resExaminationVO;
    }


}
